package com.example.jiankang.Fragment;

import static android.app.Activity.RESULT_OK;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.example.jiankang.R;
import com.example.jiankang.pages.MainActivity;
import com.example.jiankang.pages.touxiangActivity;


public class wodeFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String mParam1;
    private String mParam2;
    private ImageView imageTouxiang;

    private TextView textName;





    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_wode, container, false);

        Button tcdl = view.findViewById(R.id.button_tcdl);
        tcdl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MainActivity.class);
                startActivity(intent);
            }
        });

        imageTouxiang = view.findViewById(R.id.image_touxiang);
        imageTouxiang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(getActivity(), touxiangActivity.class), 100);

            }
        });


        textName = view.findViewById(R.id.text_name);
        textName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showEditNameDialog();
            }
        });

        return view;
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 100 && resultCode == RESULT_OK && data != null) {
            // 安全获取资源ID（不设置默认值）
            int avatarResId = data.getIntExtra("avatar_res_id", 0);
                imageTouxiang.setImageResource(avatarResId);

        }
    }

    private void showEditNameDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
        builder.setTitle("修改用户名");

        final EditText input = new EditText(requireActivity());
        input.setText(textName.getText());
        input.setSelectAllOnFocus(true);

        builder.setView(input);
        builder.setPositiveButton("确定", (dialog, which) -> {
            String newName = input.getText().toString().trim();
            if (!newName.isEmpty()) {
                textName.setText(newName);
            }
        });
        builder.setNegativeButton("取消", null);
        builder.show();
    }
}
